---
layout: "default"
keywords: ""
description: "Swift documentation for 'Range'"
root: "/v5.1"
title: "Range"
---

<div class="intro-declaration"><code class="language-swift">struct Range</code></div><p>A half-open interval from a lower bound up to, but not including, an upper
bound.</p>
<table class="standard"><tr><th id="inheritance">Inheritance</th><td><code class="inherits">CustomDebugStringConvertible, CustomReflectable, CustomStringConvertible, Equatable, RangeExpression</code></td></tr></table><div class="discussion comment"><p>You create a <code>Range</code> instance by using the half-open range operator
(<code>..&lt;</code>).</p>
<pre><code class="language-swift">let underFive = 0.0..&lt;5.0
</code></pre>
<p>You can use a <code>Range</code> instance to quickly check if a value is contained in
a particular range of values. For example:</p>
<pre><code class="language-swift">underFive.contains(3.14)
// true
underFive.contains(6.28)
// false
underFive.contains(5.0)
// false
</code></pre>
<p><code>Range</code> instances can represent an empty interval, unlike <code>ClosedRange</code>.</p>
<pre><code class="language-swift">let empty = 0.0..&lt;0.0
empty.contains(0.0)
// false
empty.isEmpty
// true
</code></pre>
<h4>Using a Range as a Collection of Consecutive Values</h4>
<p>When a range uses integers as its lower and upper bounds, or any other type
that conforms to the <code>Strideable</code> protocol with an integer stride, you can
use that range in a <code>for</code>-<code>in</code> loop or with any sequence or collection
method. The elements of the range are the consecutive values from its
lower bound up to, but not including, its upper bound.</p>
<pre><code class="language-swift">for n in 3..&lt;5 {
    print(n)
}
// Prints &quot;3&quot;
// Prints &quot;4&quot;
</code></pre>
<p>Because floating-point types such as <code>Float</code> and <code>Double</code> are their own
<code>Stride</code> types, they cannot be used as the bounds of a countable range. If
you need to iterate over consecutive floating-point values, see the
<code>stride(from:to:by:)</code> function.</p>
</div><h3>Initializers</h3><div id="init_uncheckedBounds-2f83b9366eb4f6cd4edf571509f67fb8" class="declaration"><a class="toggle-link" href="#comment-init_uncheckedBounds-2f83b9366eb4f6cd4edf571509f67fb8">init init(uncheckedBounds:)</a> <span class="required">Required</span><div class="comment collapse in" id="comment-init_uncheckedBounds-2f83b9366eb4f6cd4edf571509f67fb8"><p>Creates an instance with the given bounds.</p>
<p>Because this initializer does not perform any checks, it should be used
as an optimization only when you are absolutely certain that <code>lower</code> is
less than or equal to <code>upper</code>. Using the half-open range operator
(<code>..&lt;</code>) to form <code>Range</code> instances is preferred.</p>
<ul>
<li>Parameter bounds: A tuple of the lower and upper bounds of the range.</li>
</ul>
<h4>Declaration</h4><code class="language-swift">@inlinable public init(uncheckedBounds bounds: (lower: Bound, upper: Bound))</code></div></div><h3>Instance Variables</h3><div id="customMirror-c4bea5adb1649887b1a7e9972736e2a9" class="declaration"><a class="toggle-link" href="#comment-customMirror-c4bea5adb1649887b1a7e9972736e2a9">var customMirror</a> <span class="required">Required</span><div class="comment collapse in" id="comment-customMirror-c4bea5adb1649887b1a7e9972736e2a9"><p>The custom mirror for this instance.</p>
<p>If this type has value semantics, the mirror should be unaffected by
subsequent mutations of the instance.</p>
<h4>Declaration</h4><code class="language-swift">var customMirror: Mirror</code></div></div><div id="debugDescription-15dc1a07010ea28e38b1f4d74663fe11" class="declaration"><a class="toggle-link" href="#comment-debugDescription-15dc1a07010ea28e38b1f4d74663fe11">var debugDescription</a> <span class="required">Required</span><div class="comment collapse in" id="comment-debugDescription-15dc1a07010ea28e38b1f4d74663fe11"><p>A textual representation of the range, suitable for debugging.</p>
<h4>Declaration</h4><code class="language-swift">var debugDescription: String</code></div></div><div id="description-67daf92c833c41c95db874e18fcb2786" class="declaration"><a class="toggle-link" href="#comment-description-67daf92c833c41c95db874e18fcb2786">var description</a> <span class="required">Required</span><div class="comment collapse in" id="comment-description-67daf92c833c41c95db874e18fcb2786"><p>A textual representation of the range.</p>
<h4>Declaration</h4><code class="language-swift">var description: String</code></div></div><div id="isEmpty-371a91e5c91e40f6212ab50995ccb0cd" class="declaration"><a class="toggle-link" href="#comment-isEmpty-371a91e5c91e40f6212ab50995ccb0cd">var isEmpty</a> <span class="required">Required</span><div class="comment collapse in" id="comment-isEmpty-371a91e5c91e40f6212ab50995ccb0cd"><p>A Boolean value indicating whether the range contains no elements.</p>
<p>An empty <code>Range</code> instance has equal lower and upper bounds.</p>
<pre><code class="language-swift">let empty: Range = 10..&lt;10
print(empty.isEmpty)
// Prints &quot;true&quot;
</code></pre>
<h4>Declaration</h4><code class="language-swift">var isEmpty: Bool</code></div></div><div id="lowerBound-88584f2bd264a57a30e5142795244349" class="declaration"><a class="toggle-link" href="#comment-lowerBound-88584f2bd264a57a30e5142795244349">let lowerBound</a> <span class="required">Required</span><div class="comment collapse in" id="comment-lowerBound-88584f2bd264a57a30e5142795244349"><p>The range's lower bound.</p>
<p>In an empty range, <code>lowerBound</code> is equal to <code>upperBound</code>.</p>
<h4>Declaration</h4><code class="language-swift">let lowerBound: Bound</code></div></div><div id="upperBound-d28987cbaedb791ea498927bea6a9a90" class="declaration"><a class="toggle-link" href="#comment-upperBound-d28987cbaedb791ea498927bea6a9a90">let upperBound</a> <span class="required">Required</span><div class="comment collapse in" id="comment-upperBound-d28987cbaedb791ea498927bea6a9a90"><p>The range's upper bound.</p>
<p>In an empty range, <code>upperBound</code> is equal to <code>lowerBound</code>. A <code>Range</code>
instance does not contain its upper bound.</p>
<h4>Declaration</h4><code class="language-swift">let upperBound: Bound</code></div></div><h3>Instance Methods</h3><div id="clamped_to-7ee3ecc43b7af7866ec96cc8935ef9fb" class="declaration"><a class="toggle-link" href="#comment-clamped_to-7ee3ecc43b7af7866ec96cc8935ef9fb">func clamped(to limits: Range&lt;Bound>) -> Range&lt;Bound></a> <span class="required">Required</span><div class="comment collapse in" id="comment-clamped_to-7ee3ecc43b7af7866ec96cc8935ef9fb"><p>Returns a copy of this range clamped to the given limiting range.</p>
<p>The bounds of the result are always limited to the bounds of <code>limits</code>.
For example:</p>
<pre><code class="language-swift">let x: Range = 0..&lt;20
print(x.clamped(to: 10..&lt;1000))
// Prints &quot;10..&lt;20&quot;
</code></pre>
<p>If the two ranges do not overlap, the result is an empty range within the
bounds of <code>limits</code>.</p>
<pre><code class="language-swift">let y: Range = 0..&lt;5
print(y.clamped(to: 10..&lt;1000))
// Prints &quot;10..&lt;10&quot;
</code></pre>
<ul>
<li>Parameter limits: The range to clamp the bounds of this range.</li>
</ul>
<h4>Declaration</h4><code class="language-swift">@inlinable public func clamped(to limits: Range&lt;Bound>) -> Range&lt;Bound></code></div></div><div id="contains-6f3567afe6aa63234ecc71820249f524" class="declaration"><a class="toggle-link" href="#comment-contains-6f3567afe6aa63234ecc71820249f524">func contains(_ element: Bound) -> Bool</a> <span class="required">Required</span><div class="comment collapse in" id="comment-contains-6f3567afe6aa63234ecc71820249f524"><p>Returns a Boolean value indicating whether the given element is contained
within the range.</p>
<p>Because <code>Range</code> represents a half-open range, a <code>Range</code> instance does not
contain its upper bound. <code>element</code> is contained in the range if it is
greater than or equal to the lower bound and less than the upper bound.</p>
<ul>
<li>Parameter element: The element to check for containment.</li>
</ul>
<h4>Declaration</h4><code class="language-swift">@inlinable public func contains(_ element: Bound) -> Bool</code></div></div><div id="overlaps-b263f1d7c780d650eeff934808b68c03" class="declaration"><a class="toggle-link" href="#comment-overlaps-b263f1d7c780d650eeff934808b68c03">func overlaps(_ other: Range&lt;Bound>) -> Bool</a> <span class="required">Required</span><div class="comment collapse in" id="comment-overlaps-b263f1d7c780d650eeff934808b68c03"><p>Returns a Boolean value indicating whether this range and the given range
contain an element in common.</p>
<p>This example shows two overlapping ranges:</p>
<pre><code class="language-swift">let x: Range = 0..&lt;20
print(x.overlaps(10...1000))
// Prints &quot;true&quot;
</code></pre>
<p>Because a half-open range does not include its upper bound, the ranges
in the following example do not overlap:</p>
<pre><code class="language-swift">let y = 20..&lt;30
print(x.overlaps(y))
// Prints &quot;false&quot;
</code></pre>
<ul>
<li>Parameter other: A range to check for elements in common.</li>
</ul>
<h4>Declaration</h4><code class="language-swift">@inlinable public func overlaps(_ other: Range&lt;Bound>) -> Bool</code></div></div><div id="overlaps-b263f1d7c780d650eeff934808b68c03" class="declaration"><a class="toggle-link" href="#comment-overlaps-b263f1d7c780d650eeff934808b68c03">func overlaps(_ other: ClosedRange&lt;Bound>) -> Bool</a> <span class="required">Required</span><div class="comment collapse in" id="comment-overlaps-b263f1d7c780d650eeff934808b68c03"><h4>Declaration</h4><code class="language-swift">@inlinable public func overlaps(_ other: ClosedRange&lt;Bound>) -> Bool</code></div></div><div id="relative_to-2949b1d9b740a6772c83bb21cba0c554" class="declaration"><a class="toggle-link" href="#comment-relative_to-2949b1d9b740a6772c83bb21cba0c554">func relative(to collection: C) -> Range&lt;Bound></a> <span class="required">Required</span><div class="comment collapse in" id="comment-relative_to-2949b1d9b740a6772c83bb21cba0c554"><p>Returns the range of indices described by this range expression within
the given collection.</p>
<ul>
<li>Parameter collection: The collection to evaluate this range expression
in relation to.</li>
</ul>
<h4>Declaration</h4><code class="language-swift">@inlinable public func relative&lt;C>(to collection: C) -> Range&lt;Bound> where Bound == C.Index, C: Collection</code></div></div><h3>Type Methods</h3><div id="lhs_rhs-a606646d54567350092f2c9a8c1adc2d" class="declaration"><a class="toggle-link" href="#comment-lhs_rhs-a606646d54567350092f2c9a8c1adc2d">func !=(lhs: Self, rhs: Self) -> Bool</a> <span class="required">Required</span><div class="comment collapse in" id="comment-lhs_rhs-a606646d54567350092f2c9a8c1adc2d"><h4>Declaration</h4><code class="language-swift">public static func !=(lhs: Self, rhs: Self) -> Bool</code></div></div><div id="lhs_rhs-28a45cba8101c0b1017a22535b8b2710" class="declaration"><a class="toggle-link" href="#comment-lhs_rhs-28a45cba8101c0b1017a22535b8b2710">func ==(lhs: Range&lt;Bound>, rhs: Range&lt;Bound>) -> Bool</a> <span class="required">Required</span><div class="comment collapse in" id="comment-lhs_rhs-28a45cba8101c0b1017a22535b8b2710"><p>Returns a Boolean value indicating whether two ranges are equal.</p>
<p>Two ranges are equal when they have the same lower and upper bounds.
That requirement holds even for empty ranges.</p>
<pre><code class="language-swift">let x = 5..&lt;15
print(x == 5..&lt;15)
// Prints &quot;true&quot;

let y = 5..&lt;5
print(y == 15..&lt;15)
// Prints &quot;false&quot;
</code></pre>
<h4>Declaration</h4><code class="language-swift">@inlinable public static func ==(lhs: Range&lt;Bound>, rhs: Range&lt;Bound>) -> Bool</code></div></div><div id="pattern_value-d3d1acc155fe92facb4e9662e5491d7c" class="declaration"><a class="toggle-link" href="#comment-pattern_value-d3d1acc155fe92facb4e9662e5491d7c">func ~=(pattern: Self, value: Self.Bound) -> Bool</a> <span class="required">Required</span><div class="comment collapse in" id="comment-pattern_value-d3d1acc155fe92facb4e9662e5491d7c"><h4>Declaration</h4><code class="language-swift">@inlinable public static func ~=(pattern: Self, value: Self.Bound) -> Bool</code></div></div>